// 解决ie10，promise resolve之后then不执行的问题
import MyPromise from 'promise';
window.Promise = MyPromise;

import Vue from 'vue';
import App from './App.vue';
import router from './router/index';
import store from './store';
import './directives';
import './filters';
import actions from './utils/actions';
import moment from 'moment';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import locale from 'element-ui/lib/locale/lang/en';

if (isDemo) {
  const handleMock = require('../mock/index.js').handleMock;
  handleMock();
}

Vue.use(ElementUI, { locale });
Vue.prototype.$moment = moment;
// Vue.prototype.$moment.locale('zh_CN');

import trackHandler from './utils/trackHandler';
Vue.use(trackHandler, {
  init: true
});

const errorHandler = (error, vm) => {
  console.error('抛出全局异常');
  console.error(vm);
  console.error(error);
};

Vue.config.errorHandler = errorHandler;
Vue.prototype.$throw = error => errorHandler(error, this);

var instance = null;
if (window.__POWERD_BY_QIANKUN__) {
  __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
}

function render(props) {
  const {
    data = {},
    container,
    pathPrefix = '',
    baseAppId = '',
    token = '',
    scfpCstId = '',
    scfpUsrId = '',
    isAdmin = '',
    loginType = '',
    postMessageToBaseApp = () => {},
    styleObject = {},
    trackHandler = () => {}
  } = props || {};
  const routerWithPrefix = router(pathPrefix);
  Vue.prototype.$baseAppId = baseAppId;
  Vue.prototype.$pathPrefix = pathPrefix;
  Vue.prototype.$loginType = loginType;
  store.commit('app/setToken', token);
  store.commit('app/setBaseAppId', baseAppId);
  store.commit('app/setIsAdmin', isAdmin);
  store.commit('app/setScfpCstId', scfpCstId);
  store.commit('app/setScfpUsrId', scfpUsrId);
  store.commit('app/setStyleObject', styleObject);
  store.commit('app/setLoginType', loginType);
  window.postMessageToBaseApp = postMessageToBaseApp;
  window.trackHandler = trackHandler;

  if (props) {
    actions.setActions(props);
    actions.onGlobalStateChange((state, prev) => {
      if (
        state &&
        prev &&
        JSON.stringify(state.styleObject) != JSON.stringify(prev.styleObject)
      ) {
        Vue.prototype.$initTheme(state.styleObject);
      } else {
        store.commit('app/setGlobalState', state);
      }
    });
  }

  const mountApp = () => {
    instance = new Vue({
      router: routerWithPrefix,
      store,
      data() {
        return {
          parentRouter: data.router,
          parentVuex: data.store
        };
      },
      render: h => h(App)
    }).$mount(container ? container.querySelector('#microApp') : '#microApp');
  };

  // Vue.prototype.$initTheme().then(() => {
  mountApp();
  // });
}

export async function bootstrap() {
  console.log('vue app bootstraped');
}

export async function mount(props) {
  render(props);
}

export async function unmount() {
  instance.$destroy();
  instance.$el.innerHTML = '';
  instance = null;
}

if (!window.__POWERD_BY_QIANKUN__) {
  render();
  addNewStyle(
    '::-webkit-scrollbar-track {background: rgba(0,0,0,0.05) !important;}::-webkit-scrollbar-thumb{background:rgba(0,0,0,0.25) !important;}'
  );
}

function addNewStyle(newStyle) {
  var styleElement = document.getElementById('style_js');

  if (!styleElement) {
    styleElement = document.createElement('style');
    styleElement.type = 'text/css';
    styleElement.id = 'style_js';
    document.getElementsByTagName('head')[0].appendChild(styleElement);
  }
}
